package licode.unisop.single.user.infrastructure.assembler;

import licode.unisop.provider.vo.SiProps;
import licode.unisop.provider.vo.SiUser;
import licode.unisop.single.user.infrastructure.repository.po.UserPO;
import licode.unisop.types.dp.Gender;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 用户转换
 *
 * @author licode
 */
public class UserSingleConvert {
    public static SiUser toUserVO(UserPO userInfo) {
        SiUser user = new SiUser();

        user.setRealmId(userInfo.getRealmId());
        user.setId(userInfo.getUserId());
        user.setName(userInfo.getName());
        user.setNickName(userInfo.getNickName());
        user.setState(userInfo.getState());
        user.setPhone(userInfo.getMobile());
        user.setEditMode(userInfo.getEditMode());
        user.setUserType("");

        if (null != userInfo.getProps()) {
            user.setProps(SiProps.build(userInfo.getProps()));
        } else {
            user.setProps(new SiProps());
        }

        user.getProps().addProp("salt", userInfo.getSalt());
        user.getProps().addProp("gender", userInfo.getGender());

        user.getProps().addProp("avatar", userInfo.getAvatar());
        user.getProps().addProp("signature", userInfo.getSignature());

        user.getProps().addProp("$updateTime", userInfo.getUpdateTime());
        user.getProps().addProp("$createTime", userInfo.getCreateTime());
        user.getProps().addProp("$lastLoginTime", userInfo.getLastLoginTime());

        return user;
    }

    public static List<SiUser> toUserVO(List<UserPO> source) {
        List<SiUser> userInfos = new ArrayList<>();
        if (null != source) {
            for (UserPO item : source) {
                userInfos.add(toUserVO(item));
            }
        }

        return userInfos;
    }

    public static UserPO fromUserVO(SiUser user) {
        UserPO userPo = new UserPO();
        SiProps props = user.getProps();

        userPo.setRealmId(user.getRealmId());
        userPo.setUserId(user.getId());
        userPo.setGroupId(user.getGroupId());
        userPo.setName(null == user.getName() ? "" : user.getName());
        userPo.setState(user.getState());
        userPo.setMobile(user.getPhone() == null ? "" : user.getPhone());
        userPo.setNickName("");
        userPo.setUserType(0);
        userPo.setEditMode(user.getEditMode());
        userPo.setProps(props);

        userPo.setAvatar("");
        userPo.setSignature("");
        userPo.setSalt("");
        userPo.setGender(Gender.MALE.getGender());

        if (props != null) {
            LocalDateTime nowTime = LocalDateTime.now();

            userPo.setSalt(props.getString("salt", ""));
            userPo.setGender(props.getInt("gender", Gender.MALE.getGender()));
            userPo.setPassword(props.getString("password", ""));

            userPo.setNickName(props.getString("nickName", ""));
            userPo.setAvatar(props.getString("avatar", ""));
            userPo.setSignature(props.getString("signature", ""));

            userPo.setUpdateTime(props.getLocalDateTime("$updateTime", nowTime));
            userPo.setCreateTime(props.getLocalDateTime("$createTime", nowTime));
            userPo.setLastLoginTime(props.getLocalDateTime("$lastLoginTime", null));
        }

        return userPo;
    }
}
